﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Singer.Framework.CoreFilters;
using Singer.Shared.Modularity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Singer.Framework
{
    public static class CoreServiceExtensions
    {
        public static void AddCores(this IServiceCollection services)
        {
            services.AddControllers(opt =>
            {
                opt.Filters.Add<CoreApiExceptionFilter>();//全局异常处理过滤器
                opt.Filters.Add<CoreApiResultFilter>();//全局控制器结果处理过滤器
            }).AddNewtonsoftJson(opt =>//json格式处理
            {
                opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;//忽略循环引用
                opt.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//时间转字符串格式
                opt.SerializerSettings.Converters.Add(new StringEnumConverter());//枚举转字符串
                opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });
            services.AddSingleton<IUserContextAccessor, UserContextAccessor>();//用户信息上下文
        }
    }
}
